/*
 * Wazuh databaseFeedManager
 * Copyright (C) 2015, Wazuh Inc.
 * November 22, 2023.
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public
 * License (version 2) as published by the FSF - Free Software
 * Foundation.
 */

#ifndef _EVENT_SEND_REPORT_TEST_HPP
#define _EVENT_SEND_REPORT_TEST_HPP

#include "MockOsDataCache.hpp"
#include "MockRemediationDataCache.hpp"
#include "policyManager.hpp"
#include "socketServer.hpp"
#include "gtest/gtest.h"
#include <filesystem>

// External shared pointers definitions
extern std::shared_ptr<MockOsDataCache> spOsDataCacheMock;
extern std::shared_ptr<MockRemediationDataCache> spRemediationDataCacheMock;

/**
 * @brief SendReport test class.
 *
 */
class EventSendReportTest : public ::testing::Test
{
protected:
    /**
     * @brief Construct a new EventSendReportTest object
     *
     */
    EventSendReportTest() = default;

    /**
     * @brief Destroy the EventSendReportTest object
     *
     */
    ~EventSendReportTest() override = default;

    /**
     * @brief Fake server to receive Wazuh-DB queries.
     *
     */
    std::shared_ptr<SocketServer<Socket<OSPrimitives, SizeHeaderProtocol>, EpollWrapper>> m_fakeWdb;

    /**
     * @brief Set the environment for testing.
     *
     */
    void SetUp() override
    {
        std::filesystem::create_directories("queue/db");

        m_fakeWdb =
            std::make_shared<SocketServer<Socket<OSPrimitives, SizeHeaderProtocol>, EpollWrapper>>("queue/db/wdb");

        m_fakeWdb->listen(
            [&](const int fd, const char* data, uint32_t size, const char* dataHeader, uint32_t sizeHeader)
            {
                std::ignore = fd;
                std::ignore = dataHeader;
                std::ignore = sizeHeader;
                std::ignore = size;
                std::ignore = data;
            });

        // Policy manager initialization.
        const auto& configJson {nlohmann::json::parse(R"({
        "vulnerability-detection": {
            "enabled": "yes",
            "index-status": "yes",
            "cti-url": "cti-url.com"
        },
        "osdataLRUSize":1000,
        "clusterName":"cluster01",
        "clusterEnabled":false
        })")};
        PolicyManager::instance().initialize(configJson);
    }

    /**
     * @brief Clean the environment after testing.
     *
     */
    void TearDown() override
    {
        spOsDataCacheMock.reset();
        spRemediationDataCacheMock.reset();
        PolicyManager::instance().teardown();
        m_fakeWdb->stop();
        std::filesystem::remove_all("queue/db");
    }
};

#endif // _EVENT_SEND_REPORT_TEST_HPP
